﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Net.Mail;

namespace KPWeb
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
           // PensumFunkcje.LadujSlowniki(); //ładuj cash

        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            //wysyła maila jak bład
            Exception ex = Server.GetLastError();
            if (ex != null)
            {
                Exception baseEx = ex.GetBaseException();
                if (baseEx != null && baseEx.Message != "ASP.NET session has expired")
                {
                    try
                    {
                        if (baseEx.Message != "ASP.NET session has expired")
                        {
                            //if (HttpContext.Current.User.Identity.Name.Contains("waldemar.sztulpa") )
                            //    return;
                            MailMessage message = new MailMessage();
                            message.From = new MailAddress("SystemPensum@wum.pl");
                            message.IsBodyHtml = true;
                            message.To.Add(new MailAddress("waldemar.sztulpa@wum.edu.pl"));
                    //        message.To.Add(new MailAddress("michal.sztulpa@wum.edu.pl"));
                            string server = "sciezkę podaj!!!!";// System.Configuration.ConfigurationManager.AppSettings["cPathToRaport"].Substring(14, 5);
                            message.Subject = System.Configuration.ConfigurationManager.AppSettings["cRokAkademicki"] + " " +
                                HttpContext.Current.User.Identity.Name + "  Error KPWeb ";//+ baseEx.Message;


                            message.Body = "<table><tr><td style='background-color:lime'>Session key</td><td style='background-color:lime'>Session value</td></tr>";
                            foreach (String key in HttpContext.Current.Session.Keys)
                            {
                                message.Body += "<tr><td>" + key + "</td><td>" + HttpContext.Current.Session[key] ?? "NULL" + "</td></tr>";
                            }
                            message.Body += "</table>";
                            message.Body += "<br/>";
                            message.Body = "<table><tr><td style='background-color:lime'>HttpContext key</td><td style='background-color:lime'>HttpContext value</td></tr>";
                            foreach (String key in HttpContext.Current.Items.Keys)
                            {
                                message.Body += "<tr><td>" + key + "</td><td>" + HttpContext.Current.Items[key] ?? "NULL" + "</td></tr>";
                            }
                            message.Body += "</table>";
                            Session["Error"] = baseEx.Message;
                            string sTemp1 = "Skąd:" + server + "__" + baseEx.Message;

                            string sTemp2 = ex.ToString();



                            message.Body += "<br/><b>" + sTemp1 + "</b><br/>" + sTemp2;

                            SmtpClient client = new SmtpClient();
                            client.Host = "mx01.wum.edu.pl";
                            // client.UseDefaultCredentials = false;
                            //client.Credentials=new System.Net.NetworkCredential("
                            client.Send(message);
                        }
                    }
                    catch
                    {

                    }

                }
            }



        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

    }
}
